package com.itheima.Controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.aspect.annotation.OperLog;
import com.itheima.common.R;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import com.itheima.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * reggie-com.itheima.Controller
 * Administrator
 * 2023-03-06
 * 16:17
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R login(@RequestBody Employee employee, HttpSession session){
        //LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //lqw.eq(Employee::getUsername,employee.getUsername());
        //List<Employee> list = employeeService.list(lqw);
        //if (list.size()==0){
        //    return R.error("用户名错误");
        //}
        //String s = MD5Util.string2MD5(employee.getPassword());
        //System.out.println("加密后密码"+s);
        //lqw.eq(Employee::getPassword,s);
        //List<Employee> list1 = employeeService.list(lqw);
        //if (list1.size()==0){
        //    return R.error("密码错误");
        //}
        //if (list1.get(0).getStatus()==0){
        //    return R.error("账户被封");
        //}
        //System.out.println(list1.get(0));
        ////存到是对象 不能是集合
        //session.setAttribute("emp",list1.get(0));
        ////返回的是查询到对象 不能是集合 更不是传入的对象(只有账号密码)
        //return R.success(list1.get(0));

    //---------------------------------------方法二
        log.info("登录",employee);
        Map loginmap = employeeService.checkLogin(employee);
        Object emp = loginmap.get("emp");

        //返回登录结果
        if (emp==null){
            return R.error((String)loginmap.get("msg"));
        }
        //3.用户登录成功后，将用户信息存储到session中  注意是emp对象 不是employee和loginmap(这个里面只有用户名密码)
        session.setAttribute("emp",emp);
        return R.success(emp);
    }

    @PostMapping("/logout")
    public R logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("emp");
        //session.invalidate();
        return new R(1,null,"退出成功",null);
    }


    @OperLog(value = "新增员工",username = "admin")
    @PostMapping
    public R save(@RequestBody Employee employee){
        log.info("添加",employee);
        boolean add = employeeService.add(employee);
        if (add){
            return R.success("success");
        }
        return R.error("添加失败");

        //---------------方法二 不使用全局异常处理器 处理用户名重复问题
        //try {
        //    employeeService.add(employee);
        //} catch (Exception e) {
        //    e.getMessage();
        //    //e.printStackTrace();
        //    return R.error("添加失败");
        //}
        //return R.success("success");
    }


    @OperLog(value = "分页查询员工信息",username = "admin")
    @GetMapping("/page")
    //@RequestParam(value = "name",required = false)默认参数为必须
    public R selectByPage(@RequestParam("page") Integer page,@RequestParam("pageSize") Integer pageSize,@RequestParam(value = "name",required = false) String name ){
        Page<Employee> employeePage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        if (name!=null&&name!=""){
            lqw.like(Employee::getName,name);
            Page<Employee> page1 = employeeService.page(employeePage, lqw);
            if (page1!=null){
                Map<Object, Object> map = new HashMap<>();
                map.put("records",page1.getRecords());
                map.put("total",page1.getTotal());
                return R.success(page1);
            }
        }else {
            Page<Employee> page1 = employeeService.page(employeePage);
            if (page1!=null){
                Map<Object, Object> map = new HashMap<>();
                map.put("records",page1.getRecords());
                map.put("total",page1.getTotal());

                return R.success(map);
            }
        }

        return R.error("查询失败");
    }

    @PutMapping
    public R status(@RequestBody Employee employee){
        log.info("employee{}",employee);
        boolean update = employeeService.updateById(employee);
        if (update){
            return R.success("修改成功");
        }
        return R.error("修改失败");

    }

    @GetMapping("/{id}")
    public R selectById(@PathVariable Long id){
        Employee byId = employeeService.getById(id);
        if (byId!=null){
            return R.success(byId);
        }
        return R.error("查询失败");
    }

}
